package com.jzx.controller;

import com.google.gson.Gson;
import com.jzx.bean.User;
import com.jzx.bean.UserMessage;
import com.jzx.service.UserMessageService;
import com.jzx.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * @author JunedQQ
 * @create 2019/6/24
 * @since 1.0.0
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMessageService userMessageService;

    private Gson gson = new Gson();

    public static final String SERVER_ERROR = "服务器错误";
    public static final String SERVER_SUCCESS = "success";
    public static final String SERVER_FAILURE = "failure";
    public static final String CHARACTER_ENCODING = "UTF-8";
    public static final String NULL = "";
    public static final String DEFAULT_PHOTO = "http://120.77.205.137/back.jpg";
    public static final String DEFAULT_SIGNATURE = "这个人很懒，什么都没留下!";

    @RequestMapping(path = "/getAllUser")
    public void getAllUser(HttpServletResponse response) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询所有用户
            List<User> users = userService.findAllUser();
            //转换成json对象
            String result = gson.toJson(users);
            //传输数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/findUserById")
    public void findUserById(HttpServletResponse response, String id) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            User user = userService.findUserById(id);
            //转化成json对象
            String result = gson.toJson(user);
            //传输数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/findUserByTel")
    public void findUserByTel(HttpServletResponse response, String tel) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            User user = userService.findUserByTel(tel);
            //转化成json对象
            String result = gson.toJson(user);
            //传输数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/getUserPasswordByTel")
    public void getUserPasswordByTel(HttpServletResponse response,String tel){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            String password = userService.getUserPasswordByTel(tel);
            //传输数据
            if (password == null){
                pw.write(NULL);
            }else {
                pw.write(password);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/getUserPasswordById")
    public void getUserPasswordById(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            String password = userService.getUserPasswordById(id);
            //传输数据
            if (password == null){
                pw.write(NULL);
            }else {
                pw.write(password);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/getUserPhotoByTel")
    public void getUserPhotoByTel(HttpServletResponse response,String tel){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            String photo = userService.getUserPhotoByTel(tel);
            //传输数据
            if (photo == null){
                pw.write(NULL);
            }else {
                pw.write(photo);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/getUserPhotoById")
    public void getUserPhotoById(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            String photo = userService.getUserPhotoById(id);
            //传输数据
            if (photo == null){
                pw.write(NULL);
            }else {
                pw.write(photo);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/updateUserPasswordById")
    public void updateUserPasswordById(HttpServletResponse response,String id,String newPassword){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userService.updateUserPasswordById(Integer.parseInt(id),newPassword);
            //判断修改是否成功
            if (result == 1){
                pw.write(SERVER_SUCCESS);
            }else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/updateUserPasswordByTel")
    public void updateUserPasswordByTel(HttpServletResponse response,String tel,String newPassword){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userService.updateUserPasswordByTel(tel,newPassword);
            //判断修改是否成功
            if (result == 1){
                pw.write(SERVER_SUCCESS);
            }else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/updateUserPhotoByTel")
    public void updateUserPhotoByTel(HttpServletResponse response,String tel,String photo){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userService.updateUserPhotoByTel(tel,photo);
            //判断修改是否成功
            if (result == 1){
                pw.write(SERVER_SUCCESS);
            }else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/updateUserPhotoById")
    public void updateUserPhotoById(HttpServletResponse response,String id,String photo){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userService.updateUserPhotoById(Integer.parseInt(id),photo);
            //判断修改是否成功
            if (result == 1){
                pw.write(SERVER_SUCCESS);
            }else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/addUser")
    public void addUser(HttpServletResponse response,User user){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //填充默认头像地址
            user.setPhoto(DEFAULT_PHOTO);
            //查询用户
            int result = userService.addUser(user);
            //判断修改是否成功
            if (result == 1){
                //设置用户id
                int userId = getUserIdByTel(user.getTel());
                user.setId(userId);
                //设置用户信息
                UserMessage userMessage = new UserMessage();
                userMessage.setUserId(userId);
                userMessage.setNickName(user.getTel());
                userMessage.setSignature(DEFAULT_SIGNATURE);
                //存入用户信息
                int result2 = userMessageService.addUserMessage(userMessage);
                if (result2 == 1){
                    //转化为json对象
                    String successUser = gson.toJson(user);
                    //传输json对象
                    pw.write(successUser);
                }else {
                    pw.write(SERVER_FAILURE);
                    throw new RuntimeException("用户信息存入失败");
                }
            }else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    @RequestMapping("/getUserTotal")
    public void getUserTotal(HttpServletResponse response){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int total = userService.getUserTotal();
            //返回数据
            pw.write(String.valueOf(total));
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

    /**
     * 根据用户tel获取用户id
     * @param tel
     * @return
     */
    public int getUserIdByTel(String tel){
       return userService.getUserIdByTel(tel);
    }

    /**
     * 根据用户id获取用户tel
     * @param id
     * @return
     */
    public String getUserTelById(int id){
        return userService.getUserTelById(id);
    }

}